//
//  YiJiYinChatListViewController.m
//  MyYiJianKangApp
//
//  Created by 张国文 on 16/3/30.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "YiJiYinChatListViewController.h"
#import "AddPeopleAndGroupViewController.h"
#import "CreateGroupViewController.h"
#import "FriendApplyCell.h"
#import "MyChatViewController.h"
#import "FriendApplyVerifyViewController.h"
#import "DataBaseMananger.h"
#import "PeopleGroupApplyCell.h"
#import "GroupApplyVerifyViewController.h"

// 是否需要显示
#define kIsShouldShow @"shouldShowData"
// 好友还是群组的申请
#define kIsFriendOrGroupApply @"friendOrGroupApply"
// 发送的时间
#define kMessageSendTime @"messageSendTime"
// 群组ID
#define kGroupID @"groupID"

#define kSenderAddMessage @"kSenderAddMessage"

@interface YiJiYinChatListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UIView *blackView; // 灰色背景

@end

@implementation YiJiYinChatListViewController




- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置需要显示哪些类型的会话
    [self setDisplayConversationTypes:@[@(ConversationType_PRIVATE),
                                        @(ConversationType_DISCUSSION),
                                        @(ConversationType_CHATROOM),
                                        @(ConversationType_GROUP),
                                        @(ConversationType_APPSERVICE),
                                        @(ConversationType_SYSTEM)]];
    //设置需要将哪些类型的会话在会话列表中聚合显示
//    [self setCollectionConversationType:@[@(ConversationType_DISCUSSION),
//                                          @(ConversationType_GROUP)]];
    [self changeNavigation];
    
    // 改变头像的形状
  [RCIM sharedRCIM].globalConversationAvatarStyle = RC_USER_AVATAR_CYCLE;
}

// navi的修改
- (void)changeNavigation
{
    self.navigationItem.title = @"消息列表";
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    leftImageView.image = [UIImage imageNamed:@"houtui"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftImageView];
    UITapGestureRecognizer *leftTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lastPage:)];
    [leftImageView addGestureRecognizer:leftTapGes];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"更多" style:UIBarButtonItemStylePlain target:self action:@selector(more:)];
    UIImageView *rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    rightImageView.image = [UIImage imageNamed:@"baisejiaohao-xiaoxiliebiao"];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightImageView];
    UITapGestureRecognizer *rightTapGes = [[ UITapGestureRecognizer alloc] initWithTarget:self action:@selector(morePage:)];
    [rightImageView addGestureRecognizer:rightTapGes];
}

#pragma mark - 返回上一页面
- (void)lastPage:(UITapGestureRecognizer *)tapGes
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 更多页面
- (void)morePage:(UITapGestureRecognizer *)tapGes
{
    self.blackView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.blackView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    // 添加手势
    UITapGestureRecognizer *blackTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideTheBlackView:)];
    [self.blackView addGestureRecognizer:blackTapGes];
    UIWindow *win = [[UIApplication sharedApplication].windows lastObject];
    [win addSubview:_blackView];
    
    [self addMoreClickView];
}


#pragma mark - 隐藏灰色背景
- (void)hideTheBlackView:(UITapGestureRecognizer *)tapGes
{
    [self hideBlackView];
   
}

// 隐藏灰色背景
- (void)hideBlackView
{
    [self.blackView removeFromSuperview];
    self.blackView = nil;
}

// 建立更多的点击视图
- (void)addMoreClickView
{


    UILabel *backgroundView = [[UILabel alloc] initWithFrame:CGRectMake((kScreenWidth - 155 * kAdjustParamater), 64, 150 * kAdjustParamater, 182 * kAdjustParamater)];
    backgroundView.layer.cornerRadius = 15;
    backgroundView.layer.masksToBounds = YES;
    backgroundView.userInteractionEnabled = YES;
    backgroundView.backgroundColor = [UIColor whiteColor];
    [self.blackView addSubview:backgroundView];
    
    // 添加好友上view
    UIView *addFriendView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150 * kAdjustParamater, 60)];
    addFriendView.backgroundColor = [UIColor clearColor];
    addFriendView.userInteractionEnabled = YES;
    [backgroundView addSubview:addFriendView];
    
    UITapGestureRecognizer *addFriendTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addFriend:)];
    [addFriendView addGestureRecognizer:addFriendTapGes];
    
    // 添加小组上view
    UIView *addGroupView = [[UIView alloc] initWithFrame:CGRectMake(0, 61 * kAdjustParamater, 150 * kAdjustParamater, 60 * kAdjustParamater)];
    addGroupView.backgroundColor = [UIColor clearColor];
    addGroupView.userInteractionEnabled = YES;
    [backgroundView addSubview:addGroupView];
    
    UITapGestureRecognizer *addGroupTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addGroup:)];
    [addGroupView addGestureRecognizer:addGroupTapGes];
    
    // 创建小组上
    UIView *createGroupView = [[UIView alloc] initWithFrame:CGRectMake(0, 122 * kAdjustParamater, 150 * kAdjustParamater, 60 * kAdjustParamater)];
    createGroupView.backgroundColor = [UIColor clearColor];
    createGroupView.userInteractionEnabled = YES;
    [backgroundView addSubview:createGroupView];
    
    UITapGestureRecognizer *createGroupTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(createGroup:)];
    [createGroupView addGestureRecognizer:createGroupTapGes];
    
    // 添加好友
    UIImageView *addFriendImage = [[UIImageView alloc] initWithFrame:CGRectMake(20 * kAdjustParamater, 14 * kAdjustParamater, 30 * kAdjustParamater, 30 * kAdjustParamater)];
    addFriendImage.image = [UIImage imageNamed:@"tianjaihaoyou-xiaoxiliebiao"];
    [backgroundView addSubview:addFriendImage];
    UIButton *addFriendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    addFriendButton.frame = CGRectMake(addFriendImage.right + 10 * kAdjustParamater, 20 * kAdjustParamater, 70 * kAdjustParamater, 20 * kAdjustParamater);
    [addFriendButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    addFriendButton.userInteractionEnabled = NO;
    addFriendButton.titleLabel.font = [UIFont systemFontOfSize:16];

    [addFriendButton setTitle:@"添加好友" forState:UIControlStateNormal];
//    [addFriendButton addTarget:self action:@selector(clickChange:) forControlEvents:UIControlEventTouchUpInside];
    addFriendButton.tag = 110;
    [backgroundView addSubview:addFriendButton];

    UILabel *grayLine1 = [[UILabel alloc] initWithFrame:CGRectMake(0, addFriendImage.bottom + 16 * kAdjustParamater, 150 * kAdjustParamater, 1 * kAdjustParamater)];
    grayLine1.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    [backgroundView addSubview:grayLine1];
    
//    // 添加小组
    UIImageView *addGroupImageV = [[UIImageView alloc] initWithFrame:CGRectMake(20 * kAdjustParamater, grayLine1.bottom + 14 * kAdjustParamater, 30 * kAdjustParamater, 30 * kAdjustParamater)];
    addGroupImageV.image = [UIImage imageNamed:@"tianjiaxiaozu-xiaoxiliebiao"];
    [backgroundView addSubview:addGroupImageV];
    UIButton *addGroupButton = [UIButton buttonWithType:UIButtonTypeCustom];
    addGroupButton.frame = CGRectMake(addGroupImageV.right + 10 * kAdjustParamater, grayLine1.bottom + 20 * kAdjustParamater, 70 * kAdjustParamater, 20 * kAdjustParamater);
   
    [addGroupButton setTitle:@"添加小组" forState:UIControlStateNormal];
    addGroupButton.userInteractionEnabled = NO;
    [addGroupButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    addGroupButton.titleLabel.font = [UIFont systemFontOfSize:16];
   // [addGroupButton addTarget:self action:@selector(clickChange:) forControlEvents:UIControlEventTouchUpInside];
    addGroupButton.tag = 111;
    [backgroundView addSubview:addGroupButton];
    
    UILabel *grayLine2 = [[UILabel alloc]initWithFrame:CGRectMake(0, addGroupImageV.bottom + 16 * kAdjustParamater, 150 * kAdjustParamater, 1 * kAdjustParamater)];
    grayLine2.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    [backgroundView addSubview:grayLine2];
//
    // 创建小组
    UIImageView *createGroupImageV = [[UIImageView alloc] initWithFrame:CGRectMake(20 * kAdjustParamater, grayLine2.bottom + 14 * kAdjustParamater, 30 * kAdjustParamater, 30 * kAdjustParamater)];
    createGroupImageV.image = [UIImage imageNamed:@"chuangjianxiaozu-xiaoxiliebiao"];
    [backgroundView addSubview:createGroupImageV];
    UIButton *createGroupButton = [UIButton buttonWithType:UIButtonTypeCustom];

    createGroupButton.frame = CGRectMake(createGroupImageV.right + 10 * kAdjustParamater, grayLine2.bottom + 20 * kAdjustParamater, 70 * kAdjustParamater, 20 * kAdjustParamater);
    [createGroupButton setTitle:@"创建小组" forState:UIControlStateNormal];
    createGroupButton.userInteractionEnabled = NO;
    [createGroupButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    createGroupButton.titleLabel.font = [UIFont systemFontOfSize:16];
 //   [createGroupButton addTarget:self action:@selector(clickChange:) forControlEvents:UIControlEventTouchUpInside];
    createGroupButton.tag = 112;
    [backgroundView addSubview:createGroupButton];
    
    // 添加动画
    backgroundView.frame = CGRectMake(backgroundView.frame.origin.x, backgroundView.frame.origin.y, backgroundView.frame.size.width, 0);
    [UIView animateWithDuration:0.3 animations:^{
        backgroundView.frame = CGRectMake(backgroundView.frame.origin.x, backgroundView.frame.origin.y, backgroundView.frame.size.width, 182 * kAdjustParamater);
    } completion:^(BOOL finished) {
        
    }];

}

#pragma mark - 点击添加好友或者添加小组或者创建小组
- (void)clickChange:(UIButton *)button
{
    switch (button.tag) {
        case 110:
        {
            NSLog(@"添加好友");
            AddPeopleAndGroupViewController *addPeopleVC = [[AddPeopleAndGroupViewController alloc] init];
            addPeopleVC.friendOrGroup = @"friend";
            [self.navigationController pushViewController:addPeopleVC animated:YES];
            break;
        }
            case 111:
        {
            NSLog(@"添加小组");
            AddPeopleAndGroupViewController *addPeopleVC = [[AddPeopleAndGroupViewController alloc] init];
            addPeopleVC.friendOrGroup = @"group";
            [self.navigationController pushViewController:addPeopleVC animated:YES];
            break;
        }
            case 112:
        {
            NSLog(@"创建小组");
            CreateGroupViewController *createGroupVC = [[CreateGroupViewController alloc] init];
            [self.navigationController pushViewController:createGroupVC animated:YES];
            break;
        }
        default:
            break;
    }
    
}

#pragma mark - 添加好友
- (void)addFriend:(UITapGestureRecognizer *)tapGes
{
    NSLog(@"添加好友");
    AddPeopleAndGroupViewController *addPeopleVC = [[AddPeopleAndGroupViewController alloc] init];
    addPeopleVC.friendOrGroup = @"friend";
    [self.navigationController pushViewController:addPeopleVC animated:YES];
    [self hideBlackView];
}

#pragma mark - 添加小组
- (void)addGroup:(UITapGestureRecognizer *)tapGes
{
    NSLog(@"添加小组");
    AddPeopleAndGroupViewController *addPeopleVC = [[AddPeopleAndGroupViewController alloc] init];
    addPeopleVC.friendOrGroup = @"group";
    [self.navigationController pushViewController:addPeopleVC animated:YES];
    [self hideBlackView];
}

#pragma mark - 创建小组
- (void)createGroup:(UITapGestureRecognizer *)tapGes
{
    NSLog(@"创建小组");
    CreateGroupViewController *createGroupVC = [[CreateGroupViewController alloc] init];
    [self.navigationController pushViewController:createGroupVC animated:YES];
    [self hideBlackView];
}
















// 以下是融云的方法

#pragma mark - 将要显示数据的回调
- (NSMutableArray *)willReloadTableData:(NSMutableArray *)dataSource
{
    // 在每次从本地数据库提取数据的时候都应该提取本地自己创建数据库中展示状态为YES的数据加到当前数据源数组中
    NSArray *tempArray = [[DataBaseMananger shareManeger] getDataFromDB];
    for (int i = 0; i < tempArray.count; i++) {
        YTKKeyValueItem *item = tempArray[i];
        // 后面的为当前用户的id
        NSString *currentUserID = [[NetStatusHandle shareHandle] getCurrentLoginRongCloudUserIDByKey:item.itemId];
        if ([currentUserID isEqualToString:[[NetStatusHandle shareHandle] getCurrentLoginRongCloudUserID]]) {
            if ([[item.itemObject valueForKey:kIsShouldShow] isEqualToString:@"YES"]) {
                
                // 说明需要构建model当前数据源数组里面
                RCConversationModel *model = [[RCConversationModel alloc] init];
                model.conversationModelType = RC_CONVERSATION_MODEL_TYPE_CUSTOMIZATION;
                
                model.jsonDict = [NSDictionary dictionaryWithObjectsAndKeys:item.itemId, @"senderID", [item.itemObject valueForKey:kIsFriendOrGroupApply], kIsFriendOrGroupApply, [item.itemObject valueForKey:kMessageSendTime] , kMessageSendTime, [item.itemObject valueForKey:kGroupID], kGroupID, [item.itemObject valueForKey:kSenderAddMessage], kSenderAddMessage, nil];
                
                // 判断当前数据源之中有没有当前的model，防止重复加入，默认需要加入
                //            int result = 1;
                //            for (RCConversationModel *tempModel in self.conversationListDataSource) {
                //                int j = 0;
                //                if (tempModel.) {
                //                    <#statements#>
                //                }
                //            }
                //  [dataSource insertObject:model atIndex:0];
                [dataSource addObject:model];
            } else {
                // 说明不是申请加好友或者进群的请求的数据或者不需要显示本地自己创建的数据
            }
        }
       
    }
    
    
    
    for (int i = 0; i < dataSource.count; i++) {
        RCConversationModel *model = dataSource[i];
        if (model.conversationType == ConversationType_SYSTEM && [model.lastestMessage isMemberOfClass:[RCContactNotificationMessage class]]) {
            model.conversationModelType = RC_CONVERSATION_MODEL_TYPE_CUSTOMIZATION;
        }
        if (model.conversationType == ConversationType_SYSTEM && [model.lastestMessage isMemberOfClass:[RCTextMessage class]]) {
            model.conversationModelType = RC_CONVERSATION_MODEL_TYPE_CUSTOMIZATION;
        }
    }
    NSLog(@"dataSource = %@", dataSource);
    
    return dataSource;
}

#pragma mark - 接收到消息的回调
- (void)didReceiveMessageNotification:(NSNotification *)notification
{
   // NSNumber *left = [notification.userInfo objectForKey:@"left"];
    
    
    NSLog(@"接收到消息了");
    // 消息的实体
    RCMessage *message = notification.object;
//    NSLog(@"消息的类型 = %@", message.objectName);
//    NSLog(@"会话类型 = %lu", (unsigned long)message.conversationType);
//    NSLog(@"发送者ID：%@", message.targetId);
//    // 判断好友消息
//    if ([message.content isMemberOfClass:[RCContactNotificationMessage class]]) {
//        // 说明发送的好友消息，然后在判断是否系统会话
//        if (message.conversationType == ConversationType_SYSTEM) {
//            // 符合发送好友消息的规范
//            
//        } else {
//            NSLog(@"好友消息应该发送系统会话类型的消息");
//        }
//        [super refreshConversationTableViewIfNeeded];
//    } else {
//        // 调用刷新界面
//    //   [super didReceiveMessageNotification:notification];
//        [super refreshConversationTableViewIfNeeded];
//    }
    
    
    if (message.conversationType == ConversationType_SYSTEM) {
        if ([message.content isMemberOfClass:[RCContactNotificationMessage class]] || [message.content isMemberOfClass:[RCTextMessage class]]) {
            // 构建存储的key，前面为发送者ID，后面为接受者ID(当前用户ID)
            NSString *currentUserID = [[NetStatusHandle shareHandle] getCurrentLoginRongCloudUserID];
            NSString *saveKey = [NSString stringWithFormat:@"%@!%@", message.targetId, currentUserID];
            // 保存是发送的好友申请消息还是群组申请消息的字符串
            NSString *isFriendOrGroup = @"";
            // 保存群组名称,如果为好友消息为空
            NSString *groupName = @"";
            // 保存发送的好友消息或者群组消息的附加内容
            NSString *senderAddMessage = @"";
            // 将发送时间转化为NSString
            NSString *sendTime = [NSString stringWithFormat:@"%lld", message.sentTime];
            if ([message.content isMemberOfClass:[RCTextMessage class]]) {
                isFriendOrGroup = @"group";
                RCTextMessage *rcTM = (RCTextMessage *)message.content;
#warning 申请入组发送的消息内容
            //    NSLog(@"申请入组发送的消息内容 = %@", rcTM.content);
                groupName = rcTM.extra;
                senderAddMessage = rcTM.content;
            } else if ([message.content isMemberOfClass:[RCContactNotificationMessage class]]) {
                RCContactNotificationMessage *rcCN = (RCContactNotificationMessage *)message.content;
          //      NSLog(@"操作名 = %@", rcCN.operation);
                
#warning 好友申请发送的消息内容
             //   NSLog(@"好友申请发送的消息内容 = %@", rcCN.message);
                senderAddMessage = rcCN.message;
                isFriendOrGroup = @"friend";
                groupName = @"none";
            }
            // 判断本地自己创建的数据库中有没有和当前targetID一样的数据
            NSArray *array = [[DataBaseMananger shareManeger] getDataFromDB];
            if (array.count > 0) {
                int result = 1; // 是否需要添加数据，默认为需要
                for (int i = 0; i < array.count; i++) {
                    YTKKeyValueItem *item = array[i];
                    int j = 0; // 每次遍历是保存是否需要添加
                    if ([item.itemId isEqualToString:saveKey]) {
                        // 说明本地已经保存过此targetId用户发送过的消息了，这个时候好友消息和群组消息会相互覆盖，此时需要将本地数据库中保存的targetId的用户请求消息的展现状态改为YES
                        NSDictionary *tempDic = [NSDictionary dictionaryWithObjectsAndKeys:@"YES", kIsShouldShow, [item.itemObject valueForKey:kIsFriendOrGroupApply], kIsFriendOrGroupApply, [item.itemObject valueForKey:kMessageSendTime], kMessageSendTime, [item.itemObject valueForKey:kGroupID], kGroupID, [item.itemObject valueForKey:kSenderAddMessage], kSenderAddMessage, nil];
                        [[DataBaseMananger shareManeger] saveDataWithKey:item.itemId params:tempDic];
                //        NSLog(@"修改过后的数据存储:%@", [[DataBaseMananger shareManeger] getDataFromDB]);
                        
                        j = 0;
                    } else {
                        j = 1;
                    }
                    result = result * j;
                }
                
                // 增加数据应该for循环外面
                if (result == 1) {
                    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"NO", kIsShouldShow, isFriendOrGroup, kIsFriendOrGroupApply, sendTime, kMessageSendTime, groupName, kGroupID, senderAddMessage, kSenderAddMessage, nil];
                    [[DataBaseMananger shareManeger] saveDataWithKey:saveKey params:dic];
            //        NSLog(@"增加过后的数据存储:%@", [[DataBaseMananger shareManeger] getDataFromDB]);
                    
                }
                
            } else {
                // 本地自己创建的数据库中没有数据的时候要先加入一条数据，以发送者的ID为key
                NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"NO", kIsShouldShow, isFriendOrGroup, kIsFriendOrGroupApply, sendTime, kMessageSendTime, groupName, kGroupID, senderAddMessage, kSenderAddMessage, nil];
                [[DataBaseMananger shareManeger] saveDataWithKey:saveKey params:dic];
            //    NSLog(@"第一次存储后的数据存储:%@", [[DataBaseMananger shareManeger] getDataFromDB]);
                
            }
            [super refreshConversationTableViewIfNeeded];
        } else {
            // 说明是ConversationType_SYSTEM类型的其他消息，比如说通知消息
            if ([message.content isMemberOfClass:[RCInformationNotificationMessage class]]) {
                NSLog(@"解散群的消息");
                [super didReceiveMessageNotification:notification];
            }
        }
        
    } else {
        // 说明不是好友申请消息或者群组申请消息
        [super didReceiveMessageNotification:notification];
    }
    
}

// 自定义cell
- (RCConversationBaseCell *)rcConversationListTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RCConversationModel *tempModel = self.conversationListDataSource[indexPath.row];
    if (tempModel.conversationModelType == RC_CONVERSATION_MODEL_TYPE_CUSTOMIZATION && [tempModel.lastestMessage isMemberOfClass:[RCContactNotificationMessage class]]) {
        // 自定义好友请求的cell
        static NSString *identifier = @"friendApply";
        FriendApplyCell *cell = [[FriendApplyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSString *userName = @"";
        // 进行模拟数据的组件
        if ([tempModel.senderUserId isEqualToString:@"10000"]) {
            userName = @"张国文";
        } else if ([tempModel.senderUserId isEqualToString:@"10001"]) {
            userName = @"梅江林";
        } else if ([tempModel.senderUserId isEqualToString:@"10002"]) {
            userName = @"殷雄";
        } else if ([tempModel.senderUserId isEqualToString:@"10003"]) {
            userName = @"吕峰";
        } else if ([tempModel.senderUserId isEqualToString:@"10004"]) {
            userName = @"欧兆";
        } else if ([tempModel.senderUserId isEqualToString:@"10005"]) {
            userName = @"沈佳";
        }
        cell.haoYouLabel.text = [NSString stringWithFormat:@"好友申请消息-%@", userName];
        cell.yanZhengLabel.text = [NSString stringWithFormat:@"来自%@的好友请求", userName];
        cell.timeLabel.text = [self ConvertMessageTime:tempModel.sentTime / 1000];
        return cell;
    } else if (tempModel.conversationModelType == RC_CONVERSATION_MODEL_TYPE_CUSTOMIZATION && [tempModel.lastestMessage isMemberOfClass:[RCTextMessage class]]) {
        // 自定义群组申请的cell
        static NSString *identiifier = @"peopleGroupApply";
        PeopleGroupApplyCell *cell = [[PeopleGroupApplyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identiifier];
        // 进行模拟数据的组件
        NSString *userName = @"";
        if ([tempModel.senderUserId isEqualToString:@"10000"]) {
            userName = @"张国文";
        } else if ([tempModel.senderUserId isEqualToString:@"10001"]) {
            userName = @"梅江林";
        } else if ([tempModel.senderUserId isEqualToString:@"10002"]) {
            userName = @"殷雄";
        } else if ([tempModel.senderUserId isEqualToString:@"10003"]) {
            userName = @"吕峰";
        } else if ([tempModel.senderUserId isEqualToString:@"10004"]) {
            userName = @"欧兆";
        } else if ([tempModel.senderUserId isEqualToString:@"10005"]) {
            userName = @"沈佳";
        }
        cell.groupApplyLabel.text = [NSString stringWithFormat:@"入群申请消息-%@", userName];
        RCTextMessage *rcTM = (RCTextMessage *)tempModel.lastestMessage;
        
        cell.groupApplyContentLabel.text = [NSString stringWithFormat:@"%@想要加入%@群", userName, rcTM.extra];
        cell.timeLabel.text = [self ConvertMessageTime:tempModel.sentTime / 1000];
        return cell;
    } else if (tempModel.jsonDict != nil && tempModel.conversationModelType == RC_CONVERSATION_MODEL_TYPE_CUSTOMIZATION) {
        // 说明是本地自己创建的数据中的数据
        if ([[tempModel.jsonDict valueForKey:kIsFriendOrGroupApply] isEqualToString:@"friend"]) {
             // 说明是好友请求
            NSString *userName = @"";
            // 获取发送者ID
            NSString *senderID = [[NetStatusHandle shareHandle] getCurrentLoginRongCloudUserIDDuiYingSenderIDByKey:[tempModel.jsonDict valueForKey:@"senderID"]];
            if ([senderID isEqualToString:@"10000"]) {
                userName = @"张国文";
            } else if ([senderID isEqualToString:@"10001"]) {
                userName = @"梅江林";
            } else if ([senderID isEqualToString:@"10002"]) {
                userName = @"殷雄";
            } else if ([senderID isEqualToString:@"10003"]) {
                userName = @"吕峰";
            } else if ([senderID isEqualToString:@"10004"]) {
                userName = @"欧兆";
            } else if ([senderID isEqualToString:@"10005"]) {
                userName = @"沈佳";
            }
           
            static NSString *identifier = @"bendiFriendCell";
            FriendApplyCell *cell = [[FriendApplyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.haoYouLabel.text = [NSString stringWithFormat:@"好友申请消息-%@", userName];
            cell.yanZhengLabel.text = [NSString stringWithFormat:@"来自%@的好友请求--本地", userName];
            cell.timeLabel.text = [self ConvertMessageTime:[[tempModel.jsonDict valueForKey:kMessageSendTime] longLongValue]  / 1000];
            return cell;
        } else if ([[tempModel.jsonDict valueForKey:kIsFriendOrGroupApply] isEqualToString:@"group"]) {
            // 说明是群组申请的消息
            NSString *userName = @"";
            // 获取发送者ID
            NSString *senderID = [[NetStatusHandle shareHandle] getCurrentLoginRongCloudUserIDDuiYingSenderIDByKey:[tempModel.jsonDict valueForKey:@"senderID"]];
            if ([senderID isEqualToString:@"10000"]) {
                userName = @"张国文";
            } else if ([senderID isEqualToString:@"10001"]) {
                userName = @"梅江林";
            } else if ([senderID isEqualToString:@"10002"]) {
                userName = @"殷雄";
            } else if ([senderID isEqualToString:@"10003"]) {
                userName = @"吕峰";
            } else if ([senderID isEqualToString:@"10004"]) {
                userName = @"欧兆";
            } else if ([senderID isEqualToString:@"10005"]) {
                userName = @"沈佳";
            }
            static NSString *identifier = @"bendigroupApplyCell";
            PeopleGroupApplyCell *cell = [[PeopleGroupApplyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.groupApplyLabel.text = [NSString stringWithFormat:@"入群申请消息-%@", userName];
            
            cell.groupApplyContentLabel.text = [NSString stringWithFormat:@"%@想要加入%@群--本地", userName, [tempModel.jsonDict valueForKey:kGroupID]];
            cell.timeLabel.text = [self ConvertMessageTime:[[tempModel.jsonDict valueForKey:kMessageSendTime] longLongValue]  / 1000];
            return cell;
        }
    }
    return nil;
}

// 自定义cell的高度
- (CGFloat)rcConversationListTableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 67 * kAdjustParamater;
}

// 左滑删除数据
- (void)rcConversationListTableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    RCConversationModel *tempModel = self.conversationListDataSource[indexPath.row];
    if (tempModel.jsonDict != nil) {
        // 说明是本地自己创建数据库里的数据
        // 构建saveKey，根据saveKey进行删除本地数据
        [[DataBaseMananger shareManeger] deleteDataWithID:[tempModel.jsonDict valueForKey:@"senderID"]];
        // 从当前数据库中删除
        [self.conversationListDataSource removeObjectAtIndex:indexPath.row];
        [self.conversationListTableView reloadData];
        NSLog(@"删除过后数据 = %@", [[DataBaseMananger shareManeger] getDataFromDB]);
    } else {
        // 1、从本地数据库中删除会话
        [[RCIMClient sharedRCIMClient] removeConversation:tempModel.conversationType targetId:tempModel.targetId];
        // 从当前数据库中删除
        [self.conversationListDataSource removeObjectAtIndex:indexPath.row];
        [self.conversationListTableView reloadData];
        
        // 2、需要将本地数据清空，构建savekey
        NSArray *benDiArray = [[DataBaseMananger shareManeger] getDataFromDB];
        NSString *saveKey = [NSString stringWithFormat:@"%@!%@", tempModel.senderUserId, [[NetStatusHandle shareHandle] getCurrentLoginRongCloudUserID]];
        for (int i = 0; i < benDiArray.count; i++) {
            YTKKeyValueItem *item = benDiArray[i];
            if ([item.itemId isEqualToString:saveKey]) {
                [[DataBaseMananger shareManeger] deleteDataWithID:saveKey];
            }
        }
       
        NSLog(@"删除过后数据 = %@", [[DataBaseMananger shareManeger] getDataFromDB]);
    }
    
}

// 时间处理的方法
- (NSString *)ConvertMessageTime:(long long)secs {
    NSString *timeText = nil;
    
    NSDate *messageDate = [NSDate dateWithTimeIntervalSince1970:secs];
    
    //    DebugLog(@"messageDate==>%@",messageDate);
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    
    NSString *strMsgDay = [formatter stringFromDate:messageDate];
    
    NSDate *now = [NSDate date];
    NSString *strToday = [formatter stringFromDate:now];
    NSDate *yesterday = [[NSDate alloc] initWithTimeIntervalSinceNow:-(24 * 60 * 60)];
    NSString *strYesterday = [formatter stringFromDate:yesterday];
    
    NSString *_yesterday = nil;
    if ([strMsgDay isEqualToString:strToday]) {
        [formatter setDateFormat:@"HH':'mm"];
    } else if ([strMsgDay isEqualToString:strYesterday]) {
        _yesterday = NSLocalizedStringFromTable(@"Yesterday", @"RongCloudKit", nil);
        //[formatter setDateFormat:@"HH:mm"];
    }
    
    if (nil != _yesterday) {
        timeText = _yesterday; //[_yesterday stringByAppendingFormat:@" %@", timeText];
    } else {
        timeText = [formatter stringFromDate:messageDate];
    }
    
    return timeText;
}








#pragma mark - 以下是本界面的内容
// 点击cell时触发
- (void)onSelectedTableRow:(RCConversationModelType)conversationModelType conversationModel:(RCConversationModel *)model atIndexPath:(NSIndexPath *)indexPath
{
    RCConversationModel *tempModel = self.conversationListDataSource[indexPath.row];
    [self.conversationListTableView deselectRowAtIndexPath:indexPath animated:NO];
    if (model.conversationModelType == RC_CONVERSATION_MODEL_TYPE_CUSTOMIZATION && [model.lastestMessage isMemberOfClass:[RCContactNotificationMessage class]]) {
        // 好友申请
        FriendApplyVerifyViewController *friendApplyVerifyVC = [[FriendApplyVerifyViewController alloc] init];
//        NSString *userName = @"";
//        // 进行模拟数据的组件
//        if ([tempModel.senderUserId isEqualToString:@"10000"]) {
//            userName = @"张国文";
//        } else if ([tempModel.senderUserId isEqualToString:@"10001"]) {
//            userName = @"梅江林";
//        } else if ([tempModel.senderUserId isEqualToString:@"10002"]) {
//            userName = @"殷雄";
//        } else if ([tempModel.senderUserId isEqualToString:@"10003"]) {
//            userName = @"吕峰";
//        } else if ([tempModel.senderUserId isEqualToString:@"10004"]) {
//            userName = @"欧兆";
//        } else if ([tempModel.senderUserId isEqualToString:@"10005"]) {
//            userName = @"沈佳";
//        }
   //     friendApplyVerifyVC.applyPeopleName = userName;
        friendApplyVerifyVC.friendDataModel = [[RCConversationModel alloc] init];
        friendApplyVerifyVC.friendDataModel = tempModel;
        NSLog(@"跳转到添加好友界面");
        [self.navigationController pushViewController:friendApplyVerifyVC animated:YES];
    } else if (model.conversationModelType == RC_CONVERSATION_MODEL_TYPE_CUSTOMIZATION && [model.lastestMessage isMemberOfClass:[RCTextMessage class]]) {
        // 群组申请
        GroupApplyVerifyViewController *groupApplyVerityVC = [[GroupApplyVerifyViewController alloc] init];
        groupApplyVerityVC.groupDataModel = [[RCConversationModel alloc] init];
        groupApplyVerityVC.groupDataModel = tempModel;
        [self.navigationController pushViewController:groupApplyVerityVC animated:YES];
        NSLog(@"群组申请的消息");
    } else if (model.jsonDict != nil && [[model.jsonDict valueForKey:kIsFriendOrGroupApply]isEqualToString:@"friend"]) {
        // 本地好友
//        NSString *userName = @"";
//        // 获取发送者ID
//        NSString *senderID = [[NetStatusHandle shareHandle] getCurrentLoginRongCloudUserIDDuiYingSenderIDByKey:[tempModel.jsonDict valueForKey:@"senderID"]];
//        if ([senderID isEqualToString:@"10000"]) {
//            userName = @"张国文";
//        } else if ([senderID isEqualToString:@"10001"]) {
//            userName = @"梅江林";
//        } else if ([senderID isEqualToString:@"10002"]) {
//            userName = @"殷雄";
//        } else if ([senderID isEqualToString:@"10003"]) {
//            userName = @"吕峰";
//        } else if ([senderID isEqualToString:@"10004"]) {
//            userName = @"欧兆";
//        } else if ([senderID isEqualToString:@"10005"]) {
//            userName = @"沈佳";
//        }
        FriendApplyVerifyViewController *friendApplyVerifyVC = [[FriendApplyVerifyViewController alloc] init];
      //  friendApplyVerifyVC.applyPeopleName = userName;
        friendApplyVerifyVC.benDiFriendData = tempModel.jsonDict;
        [self.navigationController pushViewController:friendApplyVerifyVC animated:YES];
        NSLog(@"本地的好友申请消息");
    } else if (model.jsonDict != nil && [[model.jsonDict valueForKey:kIsFriendOrGroupApply] isEqualToString:@"group"]) {
        NSLog(@"本地的群组申请消息");
        GroupApplyVerifyViewController *groupApplyVerityVC = [[GroupApplyVerifyViewController alloc] init];
        groupApplyVerityVC.benDiGroupData = tempModel.jsonDict;
        [self.navigationController pushViewController:groupApplyVerityVC animated:YES];
        
    } else {
        MyChatViewController *conversationVC = [[MyChatViewController alloc] init];
        conversationVC.conversationType = model.conversationType;
        conversationVC.targetId = model.targetId;
        conversationVC.title = model.conversationTitle;
        NSLog(@"标题:%@", model.conversationTitle);
        [self.navigationController pushViewController:conversationVC animated:YES];
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
